package org.icress.fusion.moc

class TfController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        render view : '/moc/tf/list', model : [tfInstanceList: Tf.list(params), tfInstanceTotal: Tf.count()]
    }

    def create = {
        def tfInstance = new Tf()
        tfInstance.properties = params
        render view : '/moc/tf/create', model : [tfInstance: tfInstance]
    }

    def save = {
        def tfInstance = new Tf(params)
		try{
			tfInstance.save(flush: true)
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'moc.tf.label', default: 'Tf'), tfInstance.id])}"
            redirect(action: "show", id: tfInstance.id)
		}catch(org.springframework.dao.DataIntegrityViolationException  dive){

			flash.message = "Can not create!"

			render(view: "/moc/tf/create", model: [tfInstance: tfInstance])
		}
    }

    def show = {
        def tfInstance = Tf.get(params.id)
        if (!tfInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'moc.tf.label', default: 'Tf'), params.id])}"
            redirect(action: "list")
        }
        else {
            render view : '/moc/tf/show', model : [tfInstance: tfInstance]
        }
    }

    def edit = {
        def tfInstance = Tf.get(params.id)
        if (!tfInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'moc.tf.label', default: 'Tf'), params.id])}"
            redirect(action: "list")
        }
        else {
            render view : '/moc/tf/edit', model : [tfInstance: tfInstance]
        }
    }

    def update = {
        def tfInstance = Tf.get(params.id)
        if (tfInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (tfInstance.version > version) {
                    tfInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'moc.tf.label', default: 'Tf')] as Object[], "Another user has updated this Tf while you were editing")
                    render(view: "/moc/tf/edit", model: [tfInstance: tfInstance])
                    return
                }
            }
            tfInstance.properties = params
			
			tfInstance.validate()
			
			if(!tfInstance.hasErrors()){
				try{
		            tfInstance.save(flush: true)
	                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'moc.tf.label', default: 'Tf'), tfInstance.id])}"
	                redirect(action: "show", id: tfInstance.id)
				}catch(org.springframework.dao.DataIntegrityViolationException  dive){
					flash.message = "can not update"
					render(view: "/moc/tf/edit", model: [tfInstance: tfInstance])
				}
			}else{
				tfInstance.errors.allErrors.each {
					flash.message += it
				}
				render(view: "/moc/tf/edit", model: [tfInstance: tfInstance])

			}
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'moc.tf.label', default: 'Tf'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def tfInstance = Tf.get(params.id)
        if (tfInstance) {
            try {
                tfInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'moc.tf.label', default: 'Tf'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'moc.tf.label', default: 'Tf'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'moc.tf.label', default: 'Tf'), params.id])}"
            redirect(action: "list")
        }
    }
}
